package com.zxl.yunpicture.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxl.yunpicture.application.service.UserApplicationService;
import com.zxl.yunpicture.domain.user.entity.User;
import com.zxl.yunpicture.domain.user.service.UserDomainService;
import com.zxl.yunpicture.infrastructure.common.DeleteRequest;
import com.zxl.yunpicture.infrastructure.exception.BusinessException;
import com.zxl.yunpicture.infrastructure.exception.ErrorCode;
import com.zxl.yunpicture.infrastructure.exception.ThrowUtils;
import com.zxl.yunpicture.interfaces.dto.user.UserLoginRequest;
import com.zxl.yunpicture.interfaces.dto.user.UserQueryRequest;
import com.zxl.yunpicture.interfaces.dto.user.UserRegisterRequest;
import com.zxl.yunpicture.interfaces.vo.user.LoginUserVO;
import com.zxl.yunpicture.interfaces.vo.user.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;


/**
* @author 16328
 * 原则：
 * 1.应用层要调用领域服务的方法，组合实体的调用，把逻辑下沉到领域服务 和实体中
 * 2.如果当前的方法调用了其他领域服务的内容，那就不能下沉 比如这里是user的应用层 如果调用了 picture的领域服务的内容 那就不能下沉
 * 3.替controller层擦屁股，interface层所有的逻辑放在这里实现 controller只做调用
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2025-01-27 16:19:41
*/
@Service
@Slf4j
public class UserApplicationServiceImpl implements UserApplicationService {

    @Resource
    private UserDomainService userDomainService;


    /*
    现在在应用层 依次依赖
    接口层 -> 应用层 -> 领域层
    因为这个原本的方法 不涉及和其他领域的相关性，因此将校验逻辑下沉到对象中（充血模型）
    执行注册的逻辑从 领域层 中调用
     */
    /**
     * 用户注册
     * @param userRegisterRequest
     * @return
     */
    @Override
    public long userRegister(UserRegisterRequest userRegisterRequest) {
        //校验
        User.validUserRegister(userRegisterRequest);
        //执行注册
        return userDomainService.userRegister(userRegisterRequest);
    }


    /**
     * 用户登录
     * @param userLoginRequest
     * @return
     */
    @Override
    public LoginUserVO userLogin(UserLoginRequest userLoginRequest, HttpServletRequest request) {
        //校验
        User.validUserLogin(userLoginRequest);
        //执行
        return userDomainService.userLogin(userLoginRequest,request);
    }


    /**
     * 获取加密后的密码
     * @param userPassword
     * @return
     */
    @Override
    public String getEncryptPassword(String userPassword){
        return userDomainService.getEncryptPassword(userPassword);
    }

    @Override
    public User getLoginUser(HttpServletRequest request) {
        return userDomainService.getLoginUser(request);
    }


    /**
     * 获取脱敏的已登录用户信息
     *
     * @return
     */
    @Override
    public LoginUserVO getLoginUserVO(User user) {
        return userDomainService.getLoginUserVO(user);
    }

    @Override
    public UserVO getUserVO(User user) {
        return userDomainService.getUserVO(user);
    }

    /**
     * 获取脱敏后的用户列表
     * @param userList
     * @return
     */
    @Override
    public List<UserVO> getUserVOList(List<User> userList) {
        return userDomainService.getUserVOList(userList);
    }

    @Override
    public boolean userLogout(HttpServletRequest request) {
        return userDomainService.userLogout(request);
    }

    @Override
    public QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest) {
        return userDomainService.getQueryWrapper(userQueryRequest);
    }

    @Override
    public long saveUser(User userEntity) {
        //默认密码
        final String DEFAULT_PASSWORD = "12345678";
        String encryptPassword = userDomainService.getEncryptPassword(DEFAULT_PASSWORD);
        userEntity.setUserPassword(encryptPassword);
        boolean result = userDomainService.saveUser(userEntity);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return userEntity.getId();
    }

    @Override
    public User getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userDomainService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return user;
    }

    @Override
    public UserVO getUserVOById(long id) {
        return userDomainService.getUserVO(getUserById(id));
    }

    @Override
    public boolean deleteUser(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userDomainService.removeById(deleteRequest.getId());
    }

    @Override
    public void updateUser(User user) {
        boolean result = userDomainService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
    }

    @Override
    public Page<UserVO> listUserVOByPage(UserQueryRequest userQueryRequest) {
        return userDomainService.listUserVOByPage(userQueryRequest);
    }

    @Override
    public List<User> listByIds(Set<Long> userIdSet) {
        return userDomainService.listByIds(userIdSet);
    }
}





